package com.gupao.gupaoeduvippattern.javaBase.thread.day01_5;

/**
 * @author Administrator
 * @date 2020-12-06 16:15
 */
public class Bank {

    public Bank(String account, int balance){
        this.account = account;
        this.balance = balance;
    }

    private String account;

    private int balance;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }



    public synchronized void saveAccount(int money){
        System.out.println("存钱开始==>");
        //获取账户余额
        int balance = getBalance();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        balance += money;

        setBalance(balance);
        System.out.println("存款"+money+"元后，账户余额剩余："+balance+"元");

    }

    public void drawBalance(int money){
        System.out.println("取钱开始==>");
        synchronized (this){
            //获取账户余额
            int balance = getBalance();

        /*try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

            balance -= money;

            setBalance(balance);
            System.out.println("取款"+money+"元后，账户余额剩余："+balance+"元");
        }

    }


}
